// pages/regimental-commander/frozen-application/frozen-application.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uploadFileUrl: 'upload.uploadPic',
    coldEquipmentFile: [],
    coldSpaceFile: [],
    freezingEquipmentFile: [],
    freezingSpaceFile: [],
    previewSize: 50,
    formData: {
      refrigerate_equipment_photos: '',
      refrigerate_space_photo: '',
      refrigerate_status: 1,
      refrigerate_break_reason: '',
      freeze_equipment_photos: '',
      freeze_space_photo: '',
      freeze_status: 1,
      freeze_break_reason: ''
    },
    // statusText: { 1: '未申请 ', 2: '审核中', 3: '通过', 4: '失败'}
  },

  /**
   * 选中图片后上传
   * @param {Object} e 
   */
  afterRead: function (e) {
    const { url } = e.detail.file
    const { uploadFileUrl } = this.data
    const urlStr = app.getNetAddresss(uploadFileUrl)
    const _this = this
    wx.showLoading({
      title: '上传中...',
    })
    wx.uploadFile({
      url: urlStr,
      filePath: url,
      name: 'file',
      success: function(res) {
        wx.hideLoading()
        const resData = JSON.parse(res.data)
        if (resData.result == 1) {
          const { img_url } = resData.data
          const { item, formItem } = e.currentTarget.dataset
          const formData = 'formData.' + formItem
          _this.setData({
            [item]: [{url: img_url}],
            [formData]: img_url
          })
        } else {
          wx.showToast({
            title: resData.msg,
            icon: 'none'
          })
        }
      },
      fail: function() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 删除已上传的图片
   * @param {Object} e 
   */
  deleteFile: function(e) {
    const { item, formItem } = e.currentTarget.dataset
    const formData = 'formData.' + formItem
    this.setData({
      [item]: [],
      [formData]: ''
    })
  },

  /**
   * 提交申请
   * @param {Object} e 
   */
  submitApplication: function(e) {
    const { type } = e.currentTarget.dataset
    let parems = this.data.formData
    // 冷藏品
    if (type == 'cold') {
      if (!parems.refrigerate_equipment_photos) {
        wx.showToast({
          icon: 'none',
          title: '请上传设备照片',
        })
        return
      }
      if (!parems.refrigerate_space_photo) {
        wx.showToast({
          icon: 'none',
          title: '请上传冷藏空间照片',
        })
        return
      }
      parems.refrigerate_status = 2
    } else if(type == 'freeze') {
      if (!parems.freeze_equipment_photos) {
        wx.showToast({
          icon: 'none',
          title: '请上传设备照片',
        })
        return
      }
      if (!parems.freeze_space_photo) {
        wx.showToast({
          icon: 'none',
          title: '请上传冻藏空间照片',
        })
        return
      }
      parems.freeze_status = 2
    }
    const _this = this
    const url = app.getNetAddresss('plugin.package-deliver.frontend.DeliverFreezeApply.save')
    wx.showLoading({
      title: '加载中',
    })
    app._postNetWork({
      url,
      data: parems,
      success: function(res) {
        _this.getFreezeDetail()
      },
      fail: function() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 查看驳回原因
   * @param {Object} e 
   */
  viewRejectReason: function(e) {
    const { status, type, reason } = e.currentTarget.dataset
    // 不是审核失败状态，返回
    if(status != 4) return
    const typeText = type == 'freeze' ? '冻' : '藏'
    wx.showModal({
      title: `冷${typeText}品售卖资格被驳回`,
      content: reason || '',
      showCancel: false,
      confirmText: '重新申请',
      confirmColor: '#ee0a24'
    })
  },

  /**
   * 获取冻品申请详情
   */
  getFreezeDetail() {
    const url = app.getNetAddresss('plugin.package-deliver.frontend.DeliverFreezeApply.detail')
    const _this = this
    wx.showLoading({
      title: '加载中',
    })
    app._postNetWork({
      url,
      success: function(res) {
        wx.hideLoading()
        console.log(res)
        const resData = res.data
        if (resData.result == 1) {
          const {
            refrigerate_equipment_photos,
            refrigerate_space_photo,
            refrigerate_status,
            refrigerate_break_reason,
            freeze_equipment_photos,
            freeze_space_photo,
            freeze_status,
            freeze_break_reason
          } = resData.data
          // 冷藏设备
          if(refrigerate_equipment_photos) {
            _this.setData({
              'formData.refrigerate_equipment_photos': refrigerate_equipment_photos,
              coldEquipmentFile: [{url: refrigerate_equipment_photos}]
            })
          }
          // 冷藏空间
          if(refrigerate_space_photo) {
            _this.setData({
              'formData.refrigerate_space_photo': refrigerate_space_photo,
              coldSpaceFile: [{url: refrigerate_space_photo}]
            })
          }
          // 冷冻设备
          if(freeze_equipment_photos) {
            _this.setData({
              'formData.freeze_equipment_photos': freeze_equipment_photos,
              freezingEquipmentFile: [{url: freeze_equipment_photos}]
            })
          }
          // 冷冻空间
          if(freeze_space_photo) {
            _this.setData({
              'formData.freeze_space_photo': freeze_space_photo,
              freezingSpaceFile: [{url: freeze_space_photo}]
            })
          }
          _this.setData({
            'formData.refrigerate_status': refrigerate_status,
            'formData.freeze_status': freeze_status,
            'formData.refrigerate_break_reason': refrigerate_break_reason,
            'formData.freeze_break_reason': freeze_break_reason,
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: resData.msg,
          })
        }
      },
      fail: function() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getFreezeDetail()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})